package com.rocket.database;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;

import com.rocket.entity.RocketWR98;

public class RocketWR98Sql {

	private static RocketWR98Sql	instance						= null;
	private final String			TABLE_NAME						= "RocketWR98";

	private final String			KEY_ROCKETWR98_ID				= "id";
	private final String			KEY_ROCKETWR98_ANGEL			= "angel";			//角度
	private final String			KEY_ROCKETWR98_START_LENGTH	= "startlength";
	private final String			KEY_ROCKETWR98_START_HEIGHT	= "startheight";
	private final String			KEY_ROCKETWR98_END_LENGTH		= "endlength";
	private final String			KEY_ROCKETWR98_END_HEIGHT		= "endheight";
	private final String			KEY_ROCKETWR98_TOP_LENGTH		= "toplength";
	private final String			KEY_ROCKETWR98_TOP_HEIGHT		= "topheight";
	private final String			KEY_ROCKETWR98_LENGTH			= "length";

	public static synchronized RocketWR98Sql getInstance() {
		if (null == instance) {
			instance = new RocketWR98Sql();
		}
		return instance;
	}

	public void init() {
		createTable();
		initData();
	}

	private void createTable() {
		if (DataBaseOpenHelper.mDataBase != null) {
			String sql = "drop table if exists " + TABLE_NAME;
			DataBaseOpenHelper.mDataBase.execSQL(sql);
			sql = "create table if not exists " + TABLE_NAME + "( " + KEY_ROCKETWR98_ID + " INTEGER PRIMARY KEY," + KEY_ROCKETWR98_ANGEL + " INTEGER,"
					+ KEY_ROCKETWR98_START_LENGTH + " INTEGER," + KEY_ROCKETWR98_START_HEIGHT + " INTEGER,"
					+ KEY_ROCKETWR98_END_LENGTH + " INTEGER," + KEY_ROCKETWR98_END_HEIGHT + " INTEGER,"
					+ KEY_ROCKETWR98_TOP_LENGTH + " INTEGER," + KEY_ROCKETWR98_TOP_HEIGHT + " INTEGER," + KEY_ROCKETWR98_LENGTH + "  INTEGER)";
			DataBaseOpenHelper.mDataBase.execSQL(sql);
		}
	}

	public long insert(RocketWR98 rocket) {
		if (DataBaseOpenHelper.mDataBase != null) {
			ContentValues values = new ContentValues();
			values.put(KEY_ROCKETWR98_ANGEL, rocket.angle);
			values.put(KEY_ROCKETWR98_START_LENGTH, rocket.startLength);
			values.put(KEY_ROCKETWR98_START_HEIGHT, rocket.startHeight);
			values.put(KEY_ROCKETWR98_END_LENGTH, rocket.endLength);
			values.put(KEY_ROCKETWR98_END_HEIGHT, rocket.endHeight);
			values.put(KEY_ROCKETWR98_TOP_LENGTH, rocket.topLength);
			values.put(KEY_ROCKETWR98_TOP_HEIGHT, rocket.topHeight);
			values.put(KEY_ROCKETWR98_LENGTH, rocket.length);
			return DataBaseOpenHelper.mDataBase.insert(TABLE_NAME, null, values);
		}
		return 0;
	}

	public long update(RocketWR98 rocket) {
		if (DataBaseOpenHelper.mDataBase != null) {
			ContentValues values = new ContentValues();
			values.put(KEY_ROCKETWR98_ANGEL, rocket.angle);
			values.put(KEY_ROCKETWR98_START_LENGTH, rocket.startLength);
			values.put(KEY_ROCKETWR98_START_HEIGHT, rocket.startHeight);
			values.put(KEY_ROCKETWR98_END_LENGTH, rocket.endLength);
			values.put(KEY_ROCKETWR98_END_HEIGHT, rocket.endHeight);
			values.put(KEY_ROCKETWR98_TOP_LENGTH, rocket.topLength);
			values.put(KEY_ROCKETWR98_TOP_HEIGHT, rocket.topHeight);
			values.put(KEY_ROCKETWR98_LENGTH, rocket.length);
			return DataBaseOpenHelper.mDataBase.update(TABLE_NAME, values, KEY_ROCKETWR98_ID + " = ?", new String[] { String.valueOf(rocket.id) });
		}
		return 0;
	}

	public void delete(RocketWR98 rocket) {
		delete(rocket.id);
	}

	public void delete(int id) {
		if (DataBaseOpenHelper.mDataBase != null) {
			DataBaseOpenHelper.mDataBase.delete(TABLE_NAME, KEY_ROCKETWR98_ID + " = ?", new String[] { String.valueOf(id) });
		}
	}

	public ArrayList<RocketWR98> query() {
		ArrayList<RocketWR98> rockets = new ArrayList<RocketWR98>();
		if (DataBaseOpenHelper.mDataBase != null) {
			Cursor cur = null;
			try {
				DataBaseOpenHelper.mDataBase.beginTransaction();
				cur = DataBaseOpenHelper.mDataBase.query(TABLE_NAME, null, null, null, null, null, KEY_ROCKETWR98_TOP_HEIGHT);
				if (cur != null && cur.moveToFirst()) {
					do {
						RocketWR98 rocket = new RocketWR98();
						rocket.id = cur.getInt(cur.getColumnIndex(KEY_ROCKETWR98_ID));
						rocket.angle = cur.getInt(cur.getColumnIndex(KEY_ROCKETWR98_ANGEL));
						rocket.startLength = cur.getInt(cur.getColumnIndex(KEY_ROCKETWR98_START_LENGTH));
						rocket.startHeight = cur.getInt(cur.getColumnIndex(KEY_ROCKETWR98_START_HEIGHT));
						rocket.endLength = cur.getInt(cur.getColumnIndex(KEY_ROCKETWR98_END_LENGTH));
						rocket.endHeight = cur.getInt(cur.getColumnIndex(KEY_ROCKETWR98_END_HEIGHT));
						rocket.topLength = cur.getInt(cur.getColumnIndex(KEY_ROCKETWR98_TOP_LENGTH));
						rocket.topHeight = cur.getInt(cur.getColumnIndex(KEY_ROCKETWR98_TOP_HEIGHT));
						rocket.length = cur.getInt(cur.getColumnIndex(KEY_ROCKETWR98_LENGTH));
						rockets.add(rocket);
					} while (cur.moveToNext());
				}
				DataBaseOpenHelper.mDataBase.setTransactionSuccessful();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (cur != null) {
					cur.close();
					cur = null;
				}
				DataBaseOpenHelper.mDataBase.endTransaction();
			}
		}
		return rockets;
	}

	private void initData() {
		RocketWR98 rocket = new RocketWR98();

		rocket.angle = 85;
		rocket.startHeight = 4000;
		rocket.startLength = 400;
		rocket.endHeight = 7800;
		rocket.endLength = 1680;
		rocket.topHeight = 8090;
		rocket.topLength = 1090;
		rocket.length = 2500;
		insert(rocket);

		rocket.angle = 80;
		rocket.startHeight = 3890;
		rocket.startLength = 850;
		rocket.endHeight = 7500;
		rocket.endLength = 2610;
		rocket.topHeight = 7900;
		rocket.topLength = 2110;
		rocket.length = 3435;
		insert(rocket);

		rocket.angle = 75;
		rocket.startHeight = 3600;
		rocket.startLength = 1250;
		rocket.endHeight = 6900;
		rocket.endLength = 3530;
		rocket.topHeight = 7500;
		rocket.topLength = 2600;
		rocket.length = 4370;
		insert(rocket);

		rocket.angle = 70;
		rocket.startHeight = 3500;
		rocket.startLength = 1400;
		rocket.endHeight = 6380;
		rocket.endLength = 4340;
		rocket.topHeight = 6890;
		rocket.topLength = 3840;
		rocket.length = 5305;
		insert(rocket);

		rocket.angle = 65;
		rocket.startHeight = 3300;
		rocket.startLength = 1680;
		rocket.endHeight = 5925;
		rocket.endLength = 5000;
		rocket.topHeight = 6425;
		rocket.topLength = 4605;
		rocket.length = 6240;
		insert(rocket);

		rocket.angle = 60;
		rocket.startHeight = 3000;
		rocket.startLength = 2100;
		rocket.endHeight = 5225;
		rocket.endLength = 5800;
		rocket.topHeight = 5900;
		rocket.topLength = 5280;
		rocket.length = 7175;
		insert(rocket);

		rocket.angle = 55;
		rocket.startHeight = 2800;
		rocket.startLength = 2280;
		rocket.endHeight = 4700;
		rocket.endLength = 6500;
		rocket.topHeight = 5325;
		rocket.topLength = 5730;
		rocket.length = 8110;
		insert(rocket);

		rocket.angle = 50;
		rocket.startHeight = 2400;
		rocket.startLength = 2350;
		rocket.endHeight = 3400;
		rocket.endLength = 7200;
		rocket.topHeight = 4720;
		rocket.topLength = 6000;
		rocket.length = 9045;
		insert(rocket);

		rocket.angle = 45;
		rocket.startHeight = 2100;
		rocket.startLength = 2580;
		rocket.endHeight = 3200;
		rocket.endLength = 8000;
		rocket.topHeight = 4100;
		rocket.topLength = 6180;
		rocket.length = 9980;
		insert(rocket);

	}
}
